<?php 


class Vflight_model extends CI_Model{
	
	private $dbibe;
	private $wsdl="http://ws.jzinfo.sh.cn/vibe2/vflightservice.asmx?wsdl";
	//private $wsdl="http://localhost:11347/vFlightService.asmx?wsdl";
	
	function Vflight_model()
	{
		//$this->dbibe=$this->load->database('ibe', TRUE);
	    	ini_set("date.timezone","Asia/Shanghai");
	}
	
	//新增虚拟航班
	public function addvFlight($data,$user_data)
	{
		
		$vflightClass="";

		$iclass=count($data["ClassCode"]);

		for($k=0;$k< $iclass;$k++)
		{
			$vflightClass=$vflightClass.'<cinfo ClassCode="'.$data["ClassCode"][$k].'" availSeat="'.$data["AvailSeat"][$k].'" Amount="'.$data["Amount"][$k].'" ClassNote="'.$data["Note"][$k].' ClassNote" action="add" />';
		}

		$vflightXML='<?xml version="1.0" encoding="utf-8"?>'.'<data>'.'<flightGroup>'.
					'<vflight>'.
						'<cmd>add</cmd>'.
						'<where fromport="'.$data["FromPort"].'" destport="'.$data["DestPort"].'" stopports="'.$data["stopport"][0].'/'.$data["stopport"][1].'/'.$data["stopport"][2].'" />'.
						'<when flightdateS="'.$data["FlightDate1"].'" flightdateE="'.$data["FlightDate2"].'" weekday="'.implode("/",$data["WeekAvailable"]).'" departtime="'.$data["departTime"].'" arrivaltime="'.$data["ArrivalTime"].'" flightno="'.$data["FlightNo"].'" />'.
						'<other planetype="'.$data["Planetype"].'" />'.
					'</vflight>'.
					'<vClass>'.$vflightClass.'</vClass>'.
					'</flightGroup>'.'</data>';
					
					
		$client = new SoapClient($this->wsdl);  
		
		$param = array('vflightXML'=> $vflightXML,"Hashcode"=> $user_data["uadmin_hashcode"]);
		
		
		$out = $client->vFlightcmd($param); 
		$arr = $out->vFlightcmdResult;
		
		if($arr->ICode=="0000")
		{
			return true;
		}else
		{
			return false;	
		}
		
		//print_r($arr);
		
	}

	//修改虚拟航班
	public function editvFlight($data,$user_data)
	{
		$vflightClass="";

		$iclass=count($data["ClassCode"]);

		for($k=0;$k< $iclass;$k++)
		{
			$vflightClass=$vflightClass.'<cinfo ClassCode="'.$data["ClassCode"][$k].'" availSeat="'.$data["AvailSeat"][$k].'" Amount="'.$data["Amount"][$k].'" ClassNote="'.$data["Note"][$k].' ClassNote" action="add" />';
		}

		$vflightXML='<?xml version="1.0" encoding="utf-8"?>'.'<data>'.'<flightGroup>'.
					'<vflight>'.
						'<cmd>update</cmd>'.
						'<where fromport="'.$data["FromPort"].'" destport="'.$data["DestPort"].'" stopports="'.$data["stopport"][0].'/'.$data["stopport"][1].'/'.$data["stopport"][2].'" />'.
						'<when flightdateS="'.$data["FlightDate1"].'" flightdateE="'.$data["FlightDate2"].'" weekday="'.implode("/",$data["WeekAvailable"]).'" departtime="'.$data["departTime"].'" arrivaltime="'.$data["ArrivalTime"].'" flightno="'.$data["FlightNo"].'" />'.
						'<other planetype="'.$data["Planetype"].'" />'.
					'</vflight>'.
					'<vClass>'.$vflightClass.'</vClass>'.
					'</flightGroup>'.'</data>';

		$client = new SoapClient($this->wsdl);  
		$param = array('vflightXML'=> $vflightXML,"Hashcode"=> $user_data["uadmin_hashcode"]);
		
		$out = $client->vFlightcmd($param); 
		$arr = $out->vFlightcmdResult;
		
		if($arr->ICode=="0000")
		{
			return true;
		}else
		{
			return false;	
		}
	}

	//查询航班信息 
	public function queryvflight($querydata,$user_data)
	{

		$vflightXML='<?xml version="1.0" encoding="utf-8"?>'.'<data>'.'<flightGroup>'.'<vflight>'.
			'<cmd>query</cmd>'.
			'<other page="'.$querydata["pagenumber"].'" pagesize="'.$querydata["pagesize"].'" />'.
			'<where fromport="'.$querydata["FromCityCode"].'" destport="'.$querydata["DestCityCode"].'" stopports="" />'.
			'<when flightdateS="'.$querydata["FlightDate1"].'" flightdateE="'.$querydata["FlightDate2"].'" flightno="'.$querydata["FlightNo"].'" />'.'<other planetype="" />'.'</vflight>'.'<vClass />'.'</flightGroup>'.'</data>';

		$client = new SoapClient($this->wsdl);  
		$param = array('vflightXML'=> $vflightXML,"Hashcode"=> $user_data["uadmin_hashcode"]);
	
		
		$out = $client->queryvFlight($param); 
		$arr = $out->queryvFlightResult;
		$arrvflight=array();
		
		
		
		if($arr->ICode=="0000")
		{
			$i=0;
			$xml = simplexml_load_string($arr->ReturnValue);
	
			$xflightGroup=$xml->xpath("flightGroup");
			foreach ($xflightGroup as $child)
			{
				//定位到属性节点
				foreach($child->vflight->attributes() as $key => $value )
				{
					$arrvflight[$i][$key]=''.$value;					
				}
				
				//选取舱位信息
				$xvClasses=$child->xpath("vClasses/vclass");
				$arrvflight[$i]['vClasses'][0]="";
				
				
				$j=0;
				foreach($xvClasses as $child)
				{
					$tmpclass=array();
					foreach ($child->attributes() as $a => $b)
					{
						$tmpclass[$a]=''.$b;
					}
					$arrvflight[$i]['vClasses'][$j]=$tmpclass;
					$j=$j+1;
				}
				
				
					
				$i=$i+1;
			}
			
			$xmTotal=$xml->xpath("TotalCount");
			$iTotalCount=0;
			$iTotalCount=(array)$xmTotal[0];
			$iTotalCount=$iTotalCount[0];
			
			return array("result"=> $arrvflight,"TotalCount" => $iTotalCount);
		}
		else
		{
			return false;	
		}
	}
	
	//查询舱位信息
	public function queryvclass($sid,$user_data)
	{

		$vflightXML='<?xml version="1.0" encoding="utf-8"?>'.'<data>'.'<flightGroup>'.'<vflight>'.
			'<cmd>query</cmd>'.
			'<other page="1" pagesize="20" />'.
			'<where fromport="" destport="" stopports="" vcode="'.$sid.'"  />'.
			'<when flightdateS="" flightdateE="" />'.'<other/>'.'</vflight>'.'<vClass />'.'</flightGroup>'.'</data>';

		$client = new SoapClient($this->wsdl);  
		$param = array('vflightXML'=> $vflightXML,"Hashcode"=> $user_data["uadmin_hashcode"]);

		
		$out = $client->queryvFlight($param); 
		$arr = $out->queryvFlightResult;
		$arrvflight=array();
		
		
		
		if($arr->ICode=="0000")
		{
			$returnvalue=($arr->ReturnValue);
			$xml = simplexml_load_string($returnvalue);
	
				
			//选取舱位信息
			$xvClasses=$xml->xpath("flightGroup/vClasses/vclass");
			$j=0;
			foreach($xvClasses as $child)
			{
				$tmpclass=array();
				foreach ($child->attributes() as $a => $b)
				{
					$tmpclass[$a]=''.$b;
				}
				$arrvflight[$j]=$tmpclass;
				$j=$j+1;
			}
			
			return $arrvflight;
		}
		else
		{
			return false;	
		}
	}

	
	//获取虚拟航班信息
	private function get_virtualflight($querydata)
	{
		$this->dbibe->where("flightdate",$querydata["Flightdate"]);
		$this->dbibe->where("flightno",$querydata["FlightNo"]);
		$this->dbibe->where("fromport",$querydata["FromPort"]);
		$this->dbibe->where("destport",$querydata["DestPort"]);
		$this->dbibe->select("flightid,vCode");
		return $this->dbibe->get("inf_virtualflight");
	}
	
}

?>